<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Category;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class CategoryController extends Controller
{
    /**
     * 分类以树状结构显示，最大分类层数为3层
     * @param Request $request
     * @return array
     */
    public function all(Request $request)
    {
        $parent = $request->get('parent', 0);
        $items = Category::where('parent', $parent)
            ->orderBy('order')
            ->get();
        if ($parent == 0) {
            foreach ($items as $k => $item) {
                $sons = Category::where('parent', $item['id'])->get();
                foreach ($sons as $key => $son) {
                    $grandSons = Category::where('parent', $son['id'])->get();
                    $sons[$key]['sons'] = $grandSons;
                }
                $items[$k]['sons'] = $sons;
            }
        } else {
            foreach ($items as $k => $item) {
                $sons = Category::where('parent', $item['id'])->get();
                $items[$k]['sons'] = $sons;
            }
        }
        return jsonResponse(config('common.successCode'), $items);
    }

    public function create(Request $request)
    {
        $validator = $this->validate($request);
        if ($validator->fails()) {
            return jsonResponse(config('common.validatorError'), $validator);
        }

        $parent = $request->get('parent');
        if ($parent != 0) {
            $parentCategory = Category::find($parent);
            if (empty($parentCategory)) {
                return jsonResponse(41200);
            }
        }
        $title = $request->get('title');
        $desc = $request->get('desc');
        $order = $request->get('order');
        $ifShow = $request->get('ifShow');

        $category = new Category();
        $category['parent'] = $parent;
        $category['title'] = $title;
        $category['desc'] = $desc;
        $category['order'] = $order;
        $category['if_show'] = $ifShow;
        $category->save();
        return jsonResponse();
    }

    public function update(Request $request, $id)
    {
        $validator = $this->validate($request);
        if ($validator->fails()) {
            return jsonResponse(config('common.validatorError'), $validator);
        }

        $parent = $request->get('parent');
        if ($parent != 0) {
            $parentCategory = Category::find($parent);
            if (empty($parentCategory)) {
                return jsonResponse(41200);
            }
        }
        $title = $request->get('title');
        $desc = $request->get('desc');
        $order = $request->get('order');
        $ifShow = $request->get('ifShow');

        $category = Category::find($id);
        if (empty($category)) {
            return jsonResponse(40000);
        }
        $category['parent'] = $parent;
        $category['title'] = $title;
        $category['desc'] = $desc;
        $category['order'] = $order;
        $category['if_show'] = $ifShow;
        $category->save();
        return jsonResponse();
    }

    public function destroy($id)
    {
        $category = Category::find($id);
        if (empty($category)) {
            return jsonResponse(40000);
        }
        $sons = Category::where('parent', $id)->get();
        foreach ($sons as $son) {
            Category::where('parent', $son['id'])->delete();
        }
        Category::where('parent', $id)->delete();
        Category::destroy($id);
        return jsonResponse();
    }

    private function validate($request)
    {
        return Validator::make($request->all(), [
            'title' => 'required',
            'parent' => 'required',
            'if_show' => Rule::in([0, 1]),
        ], [
            'title.required' => '名称不能为空',
            'parent.required' => '父节点不能为空',
            'if_show.in' => '是否显示的值不合法',
        ]);
    }
}
